** Tables for trust analysis

	set matsize 5000 
	cap mat drop _all

* Table 1. Descriptive Statistics

	use "$directory/data/analysis.dta", clear
				
		local trust indiv_trust_note_a indiv_trust_note_g indiv_trust_note_h indiv_trust_note_f indiv_trust_note_i ///
			indiv_trust_note_c indiv_trust_note_b indiv_trust_note_d indiv_trust_note_e 
			
		local kindness indiv_trust_kind_a indiv_trust_kind_g indiv_trust_kind_h indiv_trust_kind_f indiv_trust_kind_i ///
			indiv_trust_kind_c indiv_trust_kind_b indiv_trust_kind_d indiv_trust_kind_e indiv_trust_q5_bin
			
		local risk indiv_trust_risk_1 indiv_trust_risk_2 indiv_trust_risk_3 indiv_trust_risk_4 indiv_trust_risk_5 indiv_trust_risk_6 indiv_trust_risk_7
		
		sumstats 	(hh_faultdist hh_epidist hh_fault_minimum hh_intensity c_home_des hh_district_?  hh_assets_pca_post if tag_hh == 1) ///
					(hh_slope if tag_uc == 1) ///
					(vil_aid_org_1 vil_aid_org_10 vil_aid_org_22 vil_aid_org_14  if tag_village == 1) ///
					(indiv_male indiv_age indiv_edu_primary indiv_edu_primary_f indiv_edu_primary_m if indiv_dead == 0 & indiv_age > 17 ) ///
					(`trust' `kindness' `risk' if indiv_dead == 0) ///
			using "$directory/tables/Tables_raw.xls" ///
		,  	replace title("Table 1. Descriptive Statistics") sheet("Table 1") ///
			lines(COL_NAMES 3 LAST_ROW 3)  format((SCLB0) (SCCB0 NCRR2) (SCCB0 NCRR2) (SCCB0 NCRR0) )

			
* Table 2. Distance to Faultline and Pre-Earthquake Characteristic Exogeneity

	use "$directory/data/analysis.dta", clear
	
	* Fault RHS regressions for table
			
	local theVarlist_v /// Village
		vil_t39v3 vil_t39v4 vil_t39v5 vil_t39v11 vil_t39v6 vil_edu_primary vil_fem_secondary vil_t39v23 vil_t39v18 vil_t39v22 vil_t39v21 vil_infra 
		
		local theCols ""
	qui foreach var of varlist `theVarlist_v' {
	
		xi: reg `var' vil_uc_dfl_mean vil_uc_dist_epi vil_uc_slope_mean i.hh_district if tag_village == 1 ///
			, cl(village_code)
			est sto `var'
			
			local theLabel : var label `var'
			local theCols `"`theCols' "`theLabel'" "`theLabel'""'
			
			qui sum `var'
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			}
		
	local theVarlist_i /// Individual	
		indiv_male_height indiv_female_height indiv_edu_primary_m indiv_edu_primary_f
		
	qui foreach var of varlist `theVarlist_i' {
	
		xi: reg `var' hh_faultdist hh_epidist hh_slope i.hh_district if indiv_dead == 0 & indiv_age > 21 ///
			, cl(village_code)
			est sto `var'
			
			local theLabel : var label `var'
			local theCols `"`theCols' "`theLabel'" "`theLabel'""'
			
			qui sum `var' if indiv_dead == 0 & indiv_age > 21
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			}
			
	local theVarlist_h /// Household	
		hh_stats_electricity_pre hh_water_inhouse_pre hh_perm_house_pre /// hh stats
		hh_stats_market_pre hh_stats_water_dist_pre hh_stats_medical_pre hh_stats_privateschool_pre hh_stats_govtschool_pre  ///
		hh_distance_pre_1 hh_distance_pre_2 hh_distance_pre_6 hh_distance_pre_4 hh_distance_pre_5
		
	qui foreach var of varlist `theVarlist_h' {
	
		xi: reg `var' hh_faultdist hh_epidist hh_slope i.hh_district if tag_hh == 1 & touse_shock == 1 ///
			, cl(village_code)
			est sto `var'
			
			local theLabel : var label `var'
			local theCols `"`theCols' "`theLabel'" "`theLabel'""'
			
			qui sum `var'
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			}
			
	xml_tab `theVarlist_v' ///
		`theVarlist_i'  ///
		`theVarlist_h' ///
		using "$directory/tables/Tables_raw.xls", append ///
		lines(COL_NAMES 3) format((SCLB0) (SCCB0 NCRR3)) cnames(`theCols') showeq ceq($numbering) ///
		keep(vil_uc_dfl_mean vil_uc_dist_epi vil_uc_slope_mean hh_faultdist hh_epidist hh_slope _cons) ///
		c("Constant") stats(N r2 mean) title("Table 2. Distance to Faultline and Pre-Earthquake Characteristic Exogeneity") sheet("Table 2")

		
* Table 3. Trust in Outsiders after the Earthquake

	use "$directory/data/analysis.dta", clear
	keep if touse_trust == 1
	
	local theVarlist ///
		indiv_trust_note_g indiv_trust_note_h  ///
		indiv_trust_note_e indiv_trust_q5_bin indiv_trust_note_dif
	
	local theCols ""
	qui foreach var of varlist `theVarlist' {
	
		xi: areg `var' hh_faultdist indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district  ///
			, cl(village_code) a(indiv_age)
			est sto `var'
			
			local theLabel : var label `var'
			local theCols `"`theCols' "`theLabel'""'
			
			qui sum `var'
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			}
	
	gen hh_faultdist_30 = hh_faultdist > 30
		label var hh_faultdist_30 "Breakpoint Distance > 30 km"
	gen hh_faultdist_30_i = hh_faultdist * hh_faultdist_30
		label var hh_faultdist_30_i "Breakpoint Interaction"
	
	xi: areg indiv_trust_note_dif hh_faultdist hh_faultdist_30 hh_faultdist_30_i indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district  ///
			, cl(village_code) a(indiv_age)
			est sto chow
			
			qui sum indiv_trust_note_dif
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			test hh_faultdist_30 hh_faultdist_30_i
			local F = `r(F)'
			estadd scalar chowF = `F'
			
	xi: areg indiv_trust_q5_bin hh_faultdist hh_faultdist_30 hh_faultdist_30_i indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district  ///
			, cl(village_code) a(indiv_age)
			est sto chow2
			
			qui sum indiv_trust_q5_bin
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			
			test hh_faultdist_30 hh_faultdist_30_i
			local F = `r(F)'
			estadd scalar chowF = `F'
			
	
	xilab i.hh_district 
			
	xml_tab `theVarlist' chow chow2 using "$directory/tables/Tables_raw.xls", append below ///
		lines(COL_NAMES 3 LAST_ROW 3) format((SCLB0) (SCCB0 NCRR3)) ///
		cnames(`theCols' "Difference" "Ability to Work Together") showeq ceq($numbering) ///
		c("Constant") stats(chowF N r2 mean) title("Table 3. Trust in Outsiders after the Earthquake") sheet("Table 3") drop(o.* _Ihh_distri_*)

* Table 4. Aid and Trust in Outsiders after the Earthquake: Robustness and Mechanisms

	use "$directory/data/analysis.dta", clear
	keep if touse_trust == 1

	local theVarlist ///
		indiv_trust_note_g indiv_trust_note_h indiv_trust_note_e ///
		indiv_trust_q5_bin indiv_trust_note_dif
		
		local theRegs ""
		local theCols ""
		qui foreach var of varlist `theVarlist' {
			
			qui sum `var'
			local mean = `r(mean)'
			local sd = `r(sd)'
			
			areg `var' hh_faultdist indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district  ///
				vil_aid_org_1 ///
				, cl(village_code) a(indiv_age)
				
				est sto `var'_arm
				estadd scalar mean = `mean'
				estadd scalar sd = `sd'
				
			areg `var' hh_faultdist indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district  ///
				vil_aid_org_10 ///
				, cl(village_code) a(indiv_age)
				
				est sto `var'_wes
				estadd scalar mean = `mean'
				estadd scalar sd = `sd'
				
			areg `var' hh_faultdist indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district  ///
				vil_aid_org_22 ///
				, cl(village_code) a(indiv_age)
				
				est sto `var'_oth
				estadd scalar mean = `mean'
				estadd scalar sd = `sd'
				
			areg `var' hh_faultdist indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district  ///
				vil_aid_org_1 vil_aid_org_10 vil_aid_org_22 ///
				, cl(village_code) a(indiv_age)
				
				est sto `var'_all
				estadd scalar mean = `mean'
				estadd scalar sd = `sd'
				
				local theLabel : var label `var'
				local theCols `"`theCols' "`theLabel'" "`theLabel'" "`theLabel'" "`theLabel'" "'
				local theRegs "`theRegs' `var'_arm `var'_wes `var'_oth `var'_all"
				
			}
				
	xml_tab `theRegs' using "$directory/tables/Tables_raw.xls", append below ///
		lines(COL_NAMES 3 LAST_ROW 3) format((SCLB0) (SCCB0 NCRR3)) ///
		cnames(`theCols') showeq ceq($numbering) sheet("Table 4") ///
		c("Constant") stats(N r2 mean sd) title("Table 4. Aid and Trust in Outsiders after the Earthquake - Robustness and Mechanisms") ///
			keep(hh_faultdist vil_aid_org_1 vil_aid_org_10 vil_aid_org_22) 	
			
* Table 5. Aid and Trust in Outsiders after the Earthquake: Instrumental Variables	
			
	use "$directory/data/analysis.dta", clear
	keep if touse_trust == 1
		
	local theCols ""
	
	* First Stages
	
	qui reg vil_aid_org_10 hh_faultdist ///
		indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district i.indiv_age ///
		if indiv_trust_note_h != .,  cl(village_code)
		
		local firststage_beta = _b[hh_faultdist]
		
		test hh_faultdist
		local firststage_f = r(F)
		
	qui reg vil_aid_org_10 hh_faultdist vil_aid_org_1 vil_aid_org_22 ///
		indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district i.indiv_age ///
		if indiv_trust_note_h != .,  cl(village_code)
		
		local firststage_beta_2 = _b[hh_faultdist]
		
		test hh_faultdist
		local firststage_f_2 = r(F) 
		
	qui foreach var of varlist indiv_trust_note_h indiv_trust_note_e indiv_trust_note_dif {
	
		xi: ivregress 2sls `var' (vil_aid_org_10 = hh_faultdist) indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district i.indiv_age ///
			, cl(village_code)
			est sto `var'
			
			local theLabel : var label `var'
			local theCols `"`theCols' "`theLabel'""'
			
			qui sum `var'
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			estadd scalar beta = `firststage_beta'
			estadd scalar ftest = `firststage_f'
			
		xi: ivregress 2sls `var' (vil_aid_org_10 = hh_faultdist) vil_aid_org_1 vil_aid_org_22 indiv_edu_primary hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope i.hh_district i.indiv_age ///
			, cl(village_code)
			est sto `var'_2
			
			qui sum `var'
			local mean = `r(mean)'
			estadd scalar mean = `mean'
			estadd scalar beta = `firststage_beta_2'
			estadd scalar ftest = `firststage_f_2'
			
			}
						
	xml_tab indiv_trust_note_h indiv_trust_note_e indiv_trust_note_dif indiv_trust_note_h_2 indiv_trust_note_dif_2  ///
		using "$directory/tables/Tables_raw.xls", append below ///
		lines(COL_NAMES 3 LAST_ROW 3) format((SCLB0) (SCCB0 NCRR3)) ///
		cnames("Trust - Westerners"	"Trust - Own Region" "Difference" "Trust - Westerners" "Difference" ) showeq ceq($numbering) ///
		c("Constant") stats(N r2 beta ftest mean) title("Table 5. Aid and Trust in Outsiders after the Earthquake - Instrumental Variables") sheet("Table 5") drop(o.* _Ihh_distri_* _Iindiv_age_*)

* Table 6. Trust in Outsiders after the Earthquake: Mortality Selection Extreme Bounds

	use "$directory/data/analysis.dta", clear
	keep if touse_trust == 1 
	cap mat drop theResults theResults_STARS
	
	local theVarlist ///
		indiv_trust_note_g indiv_trust_note_h ///
		indiv_trust_q5_bin indiv_trust_note_dif
		
		cap mat drop theResults
		
		qui foreach var of varlist `theVarlist' {
		
			qui su `var'
			local theMin = `r(min)'
		
			areg `var' hh_near_quake hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district  if indiv_dead != 1, cl(village_code) a(indiv_age)
				mat regdata = r(table)
				local b1		= regdata[1,1]
				local s1 		= regdata[2,1]
				local p1	 	= regdata[4,1]
					local stars1 = 0
					if `p1' < 0.1  local stars1 = 3
					if `p1' < 0.05 local stars1 = 2
					if `p1' < 0.01 local stars1 = 1
					
			preserve
					
			replace `var' = `theMin' if indiv_dead == 1 & hh_far_from_quake == 0 & indiv_adult == 1
			replace `var' = 1 if indiv_dead == 1 & hh_far_from_quake == 1 & indiv_adult == 1
			
			areg `var' hh_near_quake  hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district , cl(village_code) a(indiv_age)
			
				mat regdata = r(table)
				local b2		= regdata[1,1]
				local s2 		= regdata[2,1]
				local p2	 	= regdata[4,1]
					local stars2 = 0
					if `p2' < 0.1  local stars2 = 3
					if `p2' < 0.05 local stars2 = 2
					if `p2' < 0.01 local stars2 = 1
			
			replace `var' = 1 if indiv_dead == 1 & hh_far_from_quake == 0 & indiv_adult == 1
			replace `var' = `theMin' if indiv_dead == 1 & hh_far_from_quake == 1 & indiv_adult == 1
			
			areg `var' hh_near_quake  hh_wealth_2 hh_wealth_1 indiv_male hh_epidist hh_slope hh_fault_minimum i.hh_district , cl(village_code) a(indiv_age)
			
				mat regdata = r(table)
				local b3		= regdata[1,1]
				local s3 		= regdata[2,1]
				local p3	 	= regdata[4,1]
					local stars3 = 0
					if `p3' < 0.1  local stars3 = 3
					if `p3' < 0.05 local stars3 = 2
					if `p3' < 0.01 local stars3 = 1
	
			restore
						
			qui sum `var'
			
				local b4		= `r(mean)'
				local s4 		= `r(sd)'
			
				
			mat theResults = nullmat(theResults) \ [`b1',`b2',`b3',`b4'] \ [`s1',`s2',`s3',`s4']
			mat theResults_STARS = nullmat(theResults_STARS) \ [`stars1',`stars2',`stars3',0] \ [0,0,0,0]
			
			local theLabel : var label `var'
			local theRows `"`theRows' "`theLabel'" "." "'
					
			}
			
			mat rownames theResults = `theRows'
			mat colnames theResults = "OLS" "Manski Lower" "Manski Upper" "Mean & SD"
			
	xml_tab theResults using "$directory/tables/Tables_raw.xls", append  ///
		lines(COL_NAMES 3 LAST_ROW 3) format((SCLB0) (SCCB0 NCRR3)) ///
		title("Table 6. Trust in Outsiders after the Earthquake - Mortality Selection Extreme Bounds") sheet("Table 6") 
				
		
* Have a lovely day!
		
